﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Text
Imports System.Data.SqlClient
Imports System.Data
Imports System.Data.OleDb

Public Class Comments

    Private Shared sb As New StringBuilder()
    Public Shared Sub GetComments(ByVal pgID As Integer, ByVal literalCtr As Literal)
        GetComments(0, pgID, literalCtr)
        ' get comments of the fist level ( comments wich is not reply to other comment)
        Dim retString = sb.ToString()
        sb.Clear()
        literalCtr.Text = retString
    End Sub

    Private Shared Sub GetComments(ByVal replyTo As Integer, ByVal pgID As Integer, ByVal literalCtr As Literal)
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT Id , Beaches_FK, Title, CommentsName, CommentsBody, DateAdded FROM Comments WHERE Beaches_FK = @beaches_FK AND replyTo = @ReplyTo ORDER BY Id DESC"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.Parameters.AddWithValue("@beaches_FK", pgID)
        cmd.Parameters.AddWithValue("@ReplyTo", replyTo)
        oleDbConn.Open()
        Using dr = cmd.ExecuteReader()
            While dr.Read()
                Dim mytimeago As String = ""
                mytimeago = CalculateTime.calculateTime(dr("DateAdded"))
                sb.Append("<div class='CommentWrapper'>")
                ' open comment wrapper div 
                If replyTo <> 0 Then
                    sb.Append("<div class='Arrow'></div>")
                End If
                ' if this is a reply comment then we add the arrow 
                ' open the comment div  and add the name
                ' note that we should pass the id of the comment that we want to reply to.                              
                sb.Append("<div class='Comment'><label>Name :</label><a class='red' href='Profile.aspx?Tourist=" & Convert.ToString(dr("CommentsName")) & "'>" & Convert.ToString(dr("CommentsName")) & "</a>" & "•&nbsp;&nbsp;<a href='AddReply.aspx?replyTo=" & Convert.ToString(dr("Id")) & "&Beaches_FK=" & Convert.ToString(dr("Beaches_FK")) & "'>Reply</a> &nbsp;•&nbsp;" & mytimeago & "  <br/><i>Title</i> : " & dr("Title").ToString() & "<br/>" & dr("CommentsBody").ToString() & "</div>")
                ' add the comment and reply then close the comment div 
                GetComments(Integer.Parse(dr("Id").ToString()), pgID, literalCtr)
                ' call get comments ( reply comments for the current comment) iteratively 
                ' finally close the  comment wrapper tag .
                sb.Append("</div>")
            End While
        End Using
        oleDbConn.Close()
    End Sub

    Public Shared Sub GetMessages(ByVal touristName As String, ByVal literalCtr As Literal)
        GetMessagesNow(touristName, literalCtr)
        ' get comments of the fist level ( comments wich is not reply to other comment)
        Dim retString = sb.ToString()
        sb.Clear()
        literalCtr.Text = retString
    End Sub

    Private Shared Sub GetMessagesNow(ByVal touristName As String, ByVal literalCtr As Literal)
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT Id , TouristName, Title, CommentsName, CommentsBody, DateAdded FROM Personal_Messages WHERE TouristName = @TouristName_FK ORDER BY Id DESC"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.Parameters.AddWithValue("@TouristName_FK", touristName)
        oleDbConn.Open()
        Using dr = cmd.ExecuteReader()
            While dr.Read()
                Dim mytimeago As String = ""
                mytimeago = CalculateTime.calculateTime(dr("DateAdded"))
                sb.Append("<div class='ContentWraperMessages'>")
                ' open comment wrapper div        
                sb.Append("<div class='Comment'><label> Name : </label> <a class='red' href='Profile.aspx?Tourist=" & Convert.ToString(dr("CommentsName")) & "'>" & Convert.ToString(dr("CommentsName")) & "</a>•&nbsp; &nbsp;" & mytimeago & "  <br/><i>Title</i> : " & dr("Title").ToString() & "<br/>" & dr("CommentsBody").ToString() & "</div>")
                ' add the comment and reply then close the comment div 
                ' finally close the  comment wrapper tag .
                sb.Append("</div>")
            End While
        End Using
        oleDbConn.Close()
    End Sub
End Class
